<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ include file="../general/header.jsp" %>

<div id="header">ITEM DETAILS</div>
<%@ include file="../general/menu.jsp" %>
<div id="content">
	<h2><c:out value="${item.name}"/></h2>
	<c:if test="${not empty auction}">
		<h4>This item is on auction!</h4>
	</c:if>	
	<br />
	<span>Description: <c:out value="${item.description}" /></span>
	<br />
	<span>Price: <c:out value="${item.precio}"/></span>
	<br />
	<span>Publication Date: <fmt:formatDate type="both" value="${item.publicationDate}" /></span>
	<br />
	<span>Stock: <c:out value="${item.count}"/></span>
	<br />
	<span>Condition: <c:choose>
						<c:when test="${item.newItem == 'true'}">
							New	
						</c:when>
						<c:otherwise>
							Used
						</c:otherwise>
					</c:choose>
	</span>
	<br />
	<span>Visit Counter: <c:out value="${item.visitCounter}"/></span>
	<br />
	<span>Owner: <c:out value="${item.user.userName}"/></span>
	<br />
	<c:if test="${not empty userL}">
	<span>Positives: ${positive} - Neutrals: ${neutral} - Negatives: ${negative} </span>
	<br />
	</c:if>
	<c:if test="${not empty item.image.data}">
		<c:if test="${fn:length(item.image.data)>0}">
			<span>Image: <img width="50" height="50" src="./getImage?itemid=${item.id}" /></span>
		</c:if>
	</c:if>
	<br />
	<br />
		
	<c:if test="${not empty userL}">
		<c:if test="${not hasComplaint}">
			<span><a href="../item/complaintItem?itemid=${item.id}">Complaint Item</a></span>
			<br />
			<c:if test="${not hasRestriction}">	
				<c:choose>
					<c:when test="${not empty auction}">
						<c:if test="${not finalized}">
							<span><a href="../item/offerOnItem?itemid=${item.id}">Make an offer!</a></span>
						</c:if>	
					</c:when>
					<c:otherwise>
						<c:if test="${not item.suspended}">
							<span><a href="../item/purchaseItem?itemid=${item.id}">Buy Item</a></span>
						</c:if>
					</c:otherwise>
				</c:choose>
			</c:if>									
		</c:if>
		<c:if test="${userL==item.user}">
			<c:if test="${not item.suspended}">
				<c:if test="${not empty auction}">
					<c:if test="${not finalized}">
						<span><a href="../item/finalizeAuction?auctionid=${auction.id}">Finalize Auction</a></span>
					</c:if>
				</c:if>			
				<span><a href="../item/suspendItem?itemid=${item.id}">Suspend Item</a></span>
			</c:if>
			<c:if test="${item.suspended}">
				<span><a href="../item/unsuspendItem?itemid=${item.id}">Unsuspend Item</a></span>
			</c:if>				
		</c:if>
	</c:if>
	
	<c:if test="${not empty auction}">
		<h3>Other offers for this item:</h3>
		<table class="listas">
			<tr style="font-weight:bold;"> 
				<td width="140px">Name</td> 
				<td width="140px">Offer Price</td> 
				<td width="125px">Date</td> 				 					
			</tr>
			<c:forEach var="offer" items="${auction.offers}">
				<tr>
				<td> <c:out value="${offer.user.name}" /> </td> 
				<td> <c:out value="${offer.offerPrice}" /> </td>
				<td> <c:out value="${offer.date}" /> </td>
				</tr>	
			</c:forEach>
		</table>
	</c:if>
	<br />	
	<h3>Questions:</h3>
	<c:forEach var="q" items="${item.questions}">
	<table style="width:900px; margin: 8px auto; border: 1px solid; border-collapse:collapse;">
		<tr>
			<c:if test="${canDeleteQuestions}">
				<th width="25px" rowspan="2">
					<a href="../item/deleteQuestion?questionid=${q.id}" >
						<img style="border:0px; height:25px; width:25px;" src="../../img/delete.png" />
					</a>
				</th>
			</c:if>
			<th width="100px" rowspan="2"><fmt:formatDate type="date" value="${q.date}" /></th>
			<th width="100px" rowspan="2"><c:out value="${q.user.name}" /></th>
			<td> <c:out value="${q.question}" /> </td>
		</tr>
		<tr>
			<c:if test="${not empty q.answer}">		
				<td> <c:out value="${q.answer}" /> </td>
			</c:if>
			<c:if test="${empty q.answer}">
				<td>
					<span style='color:red;'>No answer yet</span>
					<c:if test="${canAnswer}">
					<a href="../item/answer?questionid=${q.id}">Answer</a>
					</c:if>
				</td>
			</c:if>
		</tr>	
	</table>
	</c:forEach>
	<c:if test="${canAsk}">
	<br />
	<form:form method="POST" action="../item/makeQuestion" commandName="questionForm">
		<input type="hidden" name="itemid" value="${item.id}"/>
		Make your question: 
		<form:input path="question" />
		<form:errors path="question" />
		<input type="submit" value="Send" />
	</form:form>
	</c:if>
	<br />
</div>
<br/>
<c:if test="${not empty items}">
		<div>
			<span class="inline spaced">Suggested Items:</span>
		</div>
		<br />
		<%@ include file="../item/list.jsp" %>
</c:if>
<br />
<div id="bottom">
	<span class="inline spaced">
		<a class="color" href="../general/index">Go back to index</a>
	</span>
	<span class="inline spaced">
		<a class="color" href="../item/userItemList?userid=${item.user.id}"> See more items from the same owner </a>
	</span>
	<br />
</div>
<%@ include file="../general/footer.jsp" %>
